/*
 * @Author: LVGRAPE
 * @Date: 2023-09-07 17:59:27
 * @LastEditTime: 2024-04-24 09:57:27
 * @LastEditors: lvgrape lvgrape@outlook.com
 * @Description:
 * @FilePath: \ZINO_FC_V4\ZINO\hardware\spi_bus\spi_bus.c
 * 要啥没啥，爱咋咋的
 */
#include <rtdevice.h>
#include "drv_spi.h"
#include "drv_soft_spi.h"
#include "zino.h"
#define DBG_TAG "spi_bus"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

extern rt_err_t at_sspi_device_attach(const char *bus_name, const char *device_name, gpio_type *cs_gpiox, uint16_t cs_gpio_pin);
int zino_spi_bus_init(void)
{
    LOG_I("adding spi1 bus:");
    // if (at_sspi_device_attach("spi1", "spi_nrf", GPIOC, GPIO_PINS_12) != RT_EOK)
    // {
    //     LOG_E("spi1 si24r1 attach failed!");
    // }
    // else
    // {
    //     LOG_I("spi1 si24r1 attach success!");
    // }
    // if (at_sspi_device_attach("spi1", "spi_lsm", GPIOB, GPIO_PINS_7) != RT_EOK)
    // {
    //     LOG_E("spi1 lsm6dsr attach failed!");
    // }
    // else
    // {
    //     LOG_I("spi1 lsm6dsr attach success!");
    // }
    // if (at_sspi_device_attach("spi1", "spi_mpu", GPIOC, GPIO_PINS_13) != RT_EOK)
    // {
    //     LOG_E("spi1 mpu6500 attach failed!");
    // }
    // else
    // {
    //     LOG_I("spi1 mpu6500 attach success!");
    // }
    // if (at_sspi_device_attach("spi1", "spi_spl", GPIOA, GPIO_PINS_13) != RT_EOK)
    // {
    //     LOG_E("spi1 spl06 attach failed!");
    // }
    // else
    // {
    //     LOG_I("spi1 spl06 attach success!");
    // }
    // if (at_sspi_device_attach("spi1", "spi_qmi", GPIOB, GPIO_PINS_6) != RT_EOK)
    // {
    //     LOG_E("spi1 qmi8658 attach failed!");
    // }
    // else
    // {
    //     LOG_I("spi1 qmi8658 attach success!");
    // }
    // if (at_sspi_device_attach("spi1", "spi_pmw", GPIOA, GPIO_PINS_14) != RT_EOK)
    // {
    //     LOG_E("spi1 pmw3901 attach failed!");
    // }
    // else
    // {
    //     LOG_I("spi1 pmw3901 attach success!");
    // }
    if (rt_hw_spi_device_attach("spi2", "spi_nrf", GPIOB, GPIO_PINS_12) != RT_EOK)
    {
        LOG_E("spi1 2.4G attach failed!");
    }
    else
    {
        LOG_I("spi1 2.4G attach success!");
    }
    return RT_EOK;
}

int zino_sspi_bus_init(void)
{
    zino_pwr_ctrl(PWR_CTRL_5V_ON);
    LOG_I("PWR_CTRL_5V_ON");
    // rt_thread_mdelay(1000);
    zino_pwr_ctrl(PWR_CTRL_EXT_5V_ON);
    LOG_I("PWR_CTRL_EXT_5V_ON");
    rt_thread_mdelay(100);
    LOG_I("adding sspi bus:");
    if (at_sspi_device_attach("sspi", "spi_lsm", GPIOC, GPIO_PINS_6) != RT_EOK)
    {
        LOG_E("sspi1 lsm6dsr attach failed!");
    }
    else
    {
        LOG_I("sspi1 lsm6dsr attach success!");
    }
    if (at_sspi_device_attach("sspi", "spi_spl", GPIOC, GPIO_PINS_8) != RT_EOK)
    {
        LOG_E("sspi1 spl06 attach failed!");
    }
    else
    {
        LOG_I("sspi1 spl06 attach success!");
    }
    if (at_sspi_device_attach("sspi", "spi_pmw", GPIOC, GPIO_PINS_7) != RT_EOK)
    {
        LOG_E("sspi1 pmw3901 attach failed!");
    }
    else
    {
        LOG_I("sspi1 pmw3901 attach success!");
    }

    return RT_EOK;
}

ZINO_APP_EXPORT(zino_spi_bus_init);
ZINO_APP_EXPORT(zino_sspi_bus_init);